#region Using

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Gizmox.WebGUI.Forms;
using Gizmox.WebGUI.Forms.Authentication;
using System.Globalization;
using Gizmox.WebGUI.Common.Resources;
using System.Data;

#endregion

namespace TVC
{
    public partial class frmLogon : LogonForm
    {
        #region Classes

        /// <summary>
        /// Language option class
        /// </summary>

        [Serializable()]
        private class LanguageOption
        {
            private readonly string mstrLabel;
            private readonly string mstrCulture;

            /// <summary>
            /// Creates a new <see cref="LanguageOption"/> instance.
            /// </summary>
            /// <param name="strCulture">culture.</param>
            /// <param name="strLabel">label.</param>
            internal LanguageOption(string strCulture, string strLabel)
            {
                mstrCulture = strCulture;
                mstrLabel = strLabel;
            }

            /// <summary>
            /// Gets the culture.
            /// </summary>
            /// <value></value>
            public CultureInfo Culture
            {
                get
                {
                    return new CultureInfo(mstrCulture);
                }
            }

            /// <summary>
            /// Returns the language label
            /// </summary>
            /// <returns></returns>
            public override string ToString()
            {
                return mstrLabel;
            }

        }
        #endregion Classes
        string sErr = "";
        public frmLogon()
        {
            InitializeComponent();
            mobjLabelMessage.Text = "";
            mobjTextUsername.Text = "";
            mobjTextPassword.Text = "";
        }

        private void mobjButtonClear_Click(object sender, EventArgs e)
        {
            Context.Session.IsLoggedOn = false;
            mobjLabelMessage.Text = "";
            mobjTextUsername.Text = "";
            mobjTextPassword.Text = "";
        }

        private void mobjButtonLogon_Click(object sender, EventArgs e)
        {
            //if (mobjCheckSavePassword.Checked)
            //{
            //    Context.Cookies["Username"] = this.mobjTextUsername.Text;
            //    Context.Cookies["Password"] = this.mobjTextPassword.Text;
            //    Context.Cookies["Lang"] = this.mobjComboLanguage.SelectedIndex.ToString();
            //}
            //else
            //{
            //    Context.Cookies["Username"] = "";
            //    Context.Cookies["Password"] = "";
            //    Context.Cookies["Lang"] = "0";
            //}
            //DateTimeFormatInfo.CurrentInfo.ShortDatePattern = "yyyy-MM-dd";
            mobjButtonLogon.Focus();
            BUS.PODControl podCtr = new BUS.PODControl();
            System.Globalization.CultureInfo ci = new CultureInfo("en-US");
            ci.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
            //System.Threading.Thread.CurrentThread.CurrentCulture = ci;
            if (mobjTextUsername.Text != "")
            {
                DTO.PODInfo info = podCtr.Get(mobjTextUsername.Text, ref sErr);

                if (info.USER_ID == "" || info.PASS != Convert.ToBase64String(new System.Security.Cryptography.SHA1CryptoServiceProvider().ComputeHash(Encoding.ASCII.GetBytes(mobjTextPassword.Text))))
                {
                    if (info.USER_ID == "" && mobjTextUsername.Text == "TVC" && mobjTextPassword.Text == "TVCSYS")
                    {
                        Context.Session.IsLoggedOn = true;

                        //GetMainForm().PODInfo = 
                        Context.Session["Language"] = mobjComboLanguage.SelectedItem != null ? ((LanguageOption)mobjComboLanguage.SelectedItem).Culture.ThreeLetterISOLanguageName : "eng";
                       
                        Context.CurrentUICulture = ci;
                        Context.Session["User"] = new DTO.PODInfo();
                        return;
                    }
                    mobjLabelMessage.Text = "Invalid username or password.";
                    Context.Session.IsLoggedOn = false;

                }
                else
                {
                    //GetMainForm().PODInfo = info;
                    Context.Session.IsLoggedOn = true;
                    Context.Session["User"] = info;
                    Context.Session["Language"] = mobjComboLanguage.SelectedItem != null ? ((LanguageOption)mobjComboLanguage.SelectedItem).Culture.ThreeLetterISOLanguageName : "eng";
                    Context.CurrentUICulture = ci;
                    mobjLabelMessage.Text = "";                   
                   
                }
                info.Business_ID = cboBusiness.SelectedValue.ToString();
                info.Business_Name = cboBusiness.Text;
                //MainForm.ROLE_ID = info.ROLE_ID;                
            }
            else
            {
                mobjLabelMessage.Text = "Input username!";
                Context.Session.IsLoggedOn = false;
            }
            //Context.Session.IsLoggedOn = true;
        }

        private void frmLogon_Load(object sender, EventArgs e)
        {
            //timer1.Interval = 100;
            //timer1.Start();
            mobjComboLanguage.Items.AddRange(new LanguageOption[] {new LanguageOption("en-US","English"),new LanguageOption("vi-VN","Vietnamese")
            });
            mobjComboLanguage.SelectedIndex = 0;
            BUS.CommonControl.SetConnection(Properties.Settings.Default.TVCConnectString);
            htmlLogon.Url = "/Resources/Data/Login.html";
            ImageResourceHandle imageName = new ImageResourceHandle("Abbott.gif");
            this.pictureBox1.Image = imageName;
            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            btnForgotpass.Click += btnForgotpass_Click;


            DataTable dtb = new DataTable();

            dtb.Columns.Add("ID", typeof(string));
            dtb.Columns.Add("Name", typeof(string));
            dtb.Rows.Add("HL", "Hoang Long JOC");
            dtb.Rows.Add("HV", "Hoan Vu JOC");

            cboBusiness.DataSource = dtb;
            cboBusiness.DisplayMember = "Name";
            cboBusiness.ValueMember = "ID";

            cboBusiness.SelectedValue = Properties.Settings.Default.BUSID;
        }

        void btnForgotpass_Click(object sender, EventArgs e)
        {
            Forms.frmResetPassword frm = new Forms.frmResetPassword();
            frm.Show();
        }

        //private void mobjTextUsername_KeyUp(object objSender, KeyEventArgs objArgs)
        //{
        //    if (objArgs.KeyCode == Keys.Enter)
        //        mobjTextPassword.Focus();
        //}

        private void mobjTextPassword_KeyUp(object objSender, KeyEventArgs objArgs)
        {
            //if (objArgs.KeyCode == Keys.Enter)
            //    mobjButtonLogon_Click(null, null);
        }

        private void mobjTextUsername_LostFocus(object sender, EventArgs e)
        {
            mobjTextUsername.Text = mobjTextUsername.Text.ToUpper();
            mobjTextPassword.Focus();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            ////label1.Left -= 5;
            ////if (label1.Left <= (-this.panel1.Width))
            ////{
            ////    timer1.Enabled = false;
            ////    label1.Left = 50;
            ////    timer1.Enabled = true;
            ////}
        }

    }
}